﻿using System;
using System.Collections.Generic;
using System.Text;
using com.lover.astd.common.logic;
using com.lover.astd.common.model;
using com.lover.astd.common.model.enumer;

namespace com.lover.astd.common.logicexe.economy
{
    public class FeteExe : ExeBase
    {
        public FeteExe() { _name = ConfigStrings.S_Fete; _readable = ConfigStrings.SR_Fete; }
        public override long execute()
        {
            if (_user.Level < 30) return next_day();
            int gold_available = getGoldAvailable();

            Dictionary<string, string> config = getConfig();
            if (!config.ContainsKey(ConfigStrings.enabled) || !config[ConfigStrings.enabled].ToLower().Equals("true")) return next_day();
            string[] golds = config[ConfigStrings.fete_gold].Split(',');
            for (int i = 0, len = golds.Length, len_local = _user._fete_gold.Length; i < len && i < len_local; i++)
            {
                _user._fete_gold[i] = Int32.Parse(golds[i]);
            }

            MiscMgr mgr = ServiceFactory.getMiscManager();
            if (!mgr.getFeteInfo(_proto, _logger, _user)) return next_halfhour();
            int level = _user.Level;
            // 先点完所有的小免费, 最后再点免费的大祭司, 然后再其他
            for (int i = 4; i >= 0; i--)
            {
                while (_user._fete_now_gold[i] <= 0 && _user._fete_min_levels[i] <= level)
                {
                    mgr.doFete(_proto, _logger, _user, i + 1);
                    if (!mgr.getFeteInfo(_proto, _logger, _user))
                    {
                        logInfo("获取祭祀信息出错, 半小时后继续");
                        return next_halfhour();
                    }
                }
            }
            // 判断是否用户等级>226, 如果>226, 那么先金币祭祀, 然后再大祭祀, 否则是先大祭祀, 再金币祭祀
            if (_user.Level >= 226)
            {
                // 金币小祭祀
                for (int i = _user._fete_now_gold.Length - 2; i >= 0; i--)
                {
                    while (_user._fete_now_gold[i] <= _user._fete_gold[i] && _user._fete_now_gold[i] <= gold_available && _user._fete_min_levels[i] <= level)
                    {
                        mgr.doFete(_proto, _logger, _user, i + 1);
                        if (!mgr.getFeteInfo(_proto, _logger, _user)) break;
                        gold_available = getGoldAvailable();
                    }
                }
                // 然后再点免费大祭祀
                while (_user._fete_now_free_times[5] > 0)
                {
                    mgr.doFete(_proto, _logger, _user, 6);
                    if (!mgr.getFeteInfo(_proto, _logger, _user))
                    {
                        logInfo("获取祭祀信息出错, 半小时后继续");
                        return next_halfhour();
                    }
                }
            }
            else
            {
                // 然后再点大的祭祀
                while (_user._fete_now_free_times[5] > 0)
                {
                    mgr.doFete(_proto, _logger, _user, 6);
                    if (!mgr.getFeteInfo(_proto, _logger, _user))
                    {
                        logInfo("获取祭祀信息出错, 半小时后继续");
                        return next_halfhour();
                    }
                }
                // 金币祭祀
                for (int i = _user._fete_now_gold.Length - 1; i >= 0; i--)
                {
                    while (_user._fete_now_gold[i] <= _user._fete_gold[i] && _user._fete_now_gold[i] <= gold_available && _user._fete_min_levels[i] <= level)
                    {
                        mgr.doFete(_proto, _logger, _user, i + 1);
                        if (!mgr.getFeteInfo(_proto, _logger, _user)) break;
                        gold_available = getGoldAvailable();
                    }
                }
            }
            
            if (_user.isActivityRunning(ActivityType.FeteEvent))
            {
                int fete_event_result = mgr.getFeteEventInfo(_proto, _logger, _user);
                if (fete_event_result == 2) return next_day();
            }
            return next_day();
        }
    }
}
